home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9209.ARJ / 1009044A < prev    next >
Text File  |  1992-04-03  |  3KB  |  130 lines

  1. /***                 LISTING 2                ***/
  2. /***                                          ***/
  3. /***                   set.c                  ***/
  4. /*** **************************************** ***/
  5. /***          SET SERIAL PARAMETERS           ***/
  6. /*** **************************************** ***/
  7. #include "serial.h"
  8.  
  9. int portbase;
  10.  
  11. int SetSerial (int ComPort,long Baud,int Parity,
  12.                int DataBits,int StopBits)
  13. {
  14.   if (SetPort(ComPort))
  15.      {
  16.       return (-1);
  17.      }
  18.  
  19.   if (SetBaud(Baud))
  20.      {
  21.       return (-1);
  22.      }
  23.  
  24.   if (SetOthers(Parity,DataBits,StopBits))
  25.      {
  26.       return (-1);
  27.      }
  28.  
  29. return (0);
  30. }
  31.  
  32. /*** **************************************** ***/
  33. /***  SET THE PORT VALUE TO CORRECT COM PORT  ***/
  34. /*** **************************************** ***/
  35.  
  36. int SetPort (int ComPort)
  37. {
  38.   switch (ComPort)
  39.     {
  40.     case COM1:   portbase = COM1BASE;
  41.              break;
  42.  
  43.     case COM2:   portbase = COM2BASE;
  44.              break;
  45.  
  46.     case COM3:   portbase = COM3BASE;
  47.              break;
  48.  
  49.     case COM4:   portbase = COM4BASE;
  50.              break;
  51.  
  52.     default:     return (-1);
  53.     }
  54.   return (0);
  55. }
  56.  
  57. /*** **************************************** ***/
  58. /***  SET THE BAUD RATE - DLAB MUST BE SET ON.***/
  59. /*** **************************************** ***/
  60. int SetBaud (long Baud)
  61. {
  62.   char Current_Value;
  63.   int  divisor;
  64.  
  65.   if ((Baud <= 0) || (Baud > 115200L))
  66.     {
  67.      return (-1);
  68.     }
  69.  
  70.   else
  71.     {
  72.      divisor = (int)(115200L/Baud);
  73.     }
  74.  
  75.  /*** READ CURRENT VALUES IN LINE CONTROL REG ***/
  76.  
  77.    Current_Value = inp(portbase + LCR);
  78.  
  79.  /*** OR CURRENT VALUE WITH 0x80 TO SET DLAB  ***/
  80.  
  81.    outp(portbase + LCR,(Current_Value | DLAB));
  82.  
  83.  /***    OUTPUT BAUD DIVISOR TO LOW BYTE      ***/
  84.  
  85.    outp(portbase + DLL,(divisor & 0x00FF));
  86.  
  87.  /***   OUTPUT BAUD DIVISOR TO HIGH BYTE      ***/
  88.  
  89.    outp(portbase + DLH,((divisor >> 8) & 0x00FF));
  90.  
  91.  /*** TURN DLAB OFF BY SENDING CURRENT VALUE  ***/
  92.  
  93.    outp(portbase + LCR,Current_Value);
  94.  
  95. return (0);
  96. }
  97.  
  98. /*** **************************************** ***/
  99. /***   SET PARITY, DATA BITS, AND STOPBITS    ***/
  100. /*** **************************************** ***/
  101. int SetOthers (int Parity,int DataBits,int StopBits)
  102. {
  103.   int setting;
  104.  
  105.   if ((DataBits < 5) || (DataBits > 8))
  106.      {
  107.       return (-1);
  108.      }
  109.  
  110.   if ((StopBits != 1 && StopBits != 2))
  111.      {
  112.       return (-1);
  113.      }
  114.  
  115.   if ((Parity != NO_PARITY) && (Parity != ODD_PARITY)
  116.             && (Parity != EVEN_PARITY))
  117.      {
  118.       return (-1);
  119.      }
  120.  
  121.   setting = DataBits - 5;
  122.   setting |= ((StopBits == 1) ? 0x00 : 0x04);
  123.   setting |= Parity;
  124.  
  125.   outp(portbase + LCR,setting);
  126.  
  127.   return (0);
  128. }
  129.  
  130.